<script setup>

import {onMounted, ref} from "vue";
import {
  deleteProductApi,
  getContactApi,
  getProductsByOwnerId,
  resellApi, updateProductApi,
  uploadImgApi,
  uploadProductApi
} from "@/js/apiHelper";
import {timeCorrect} from "@/js/jsHelper";

  const newProduct = ref({
    productTitle: '',
    productPrice: '',
    productImgs: '',
    productIntro: '',
    location: ''
  })
  /**
   * 上传单张图片
   */
  const fileInput = ref()
  const uploadImg = () => {
    fileInput.value.click()
  }
  const handleUploadImg = (e)=>{
    const selectedFile = e.target.files[0]
    const formData = new FormData()
    formData.append('file', selectedFile)
    const result = uploadImgApi(formData)
    result.then(res => {
      newProduct.value.productImgs = res.data
    })
  }
  /**
   * 发布闲置
   */
  const publishProduct = ()=>{
    if(newProduct.value.productTitle === '' || newProduct.value.productPrice === '' || newProduct.value.productImgs === '' || newProduct.value.productIntro === '' || newProduct.value.location === ''){
      alert('请填写完整信息')
      return
    }
    const result = uploadProductApi(newProduct.value)
    result.then(res=>{
      if(res.data){
        alert('发布成功')
        newProduct.value = {
          productTitle: '',
          productPrice: '',
          productImgs: '',
          productIntro: '',
          location: ''
        }
        refreshAll()
      }else{
        console.log(res)
      }
    })
  }

  /**
   * 已发布
   */
  const products = ref([])
  //查询下一页
  const pageParams = ref({
        startIndex:-10,
        length:10
      })
  onMounted(()=>{
    appendProducts()
  })
  const appendProducts = ()=>{
    pageParams.value.startIndex += pageParams.value.length
    const productsResult = getProductsByOwnerId(pageParams.value.startIndex, pageParams.value.length)
    productsResult.then((res) => {
      if (res.data.length <= 0){
        pageParams.value.startIndex -= pageParams.value.length
        alert('没有更多数据了')
        return
      }
      res.data = res.data.filter((item) => item.status <= 2)
      for (const datum of res.data) {
        datum.publishTime = timeCorrect(datum.publishTime)
        products.value.push(datum)
      }
    }).catch(e=>{
      console.log('获取失败'+e)
    })
  }
  //删除
  const deleteProduct =(productId)=>{
    const result = deleteProductApi(productId)
    result.then(res=>{
      if (res.data){
        alert('删除成功')
        products.value = products.value.filter((item) => item.productId !== productId)
      }else {
        alert('删除失败')
      }
    })
  }
  //重新出售
  const reSell = (productId)=>{
    const result = resellApi(productId)
    result.then(res=>{
      if (res.data){
        alert('重设成功')
        refreshAll()
      }
    })
  }
  //联系买家
  const contactWithSeller = (productId)=>{
    const result = getContactApi(productId)
    result.then(res=>{
      alert('卖家电话号码：'+res.data.customerPhone)
    })
  }
  //更新数据
  const updateProduct = (index)=>{
    const newProduct = JSON.parse(JSON.stringify(products.value[index]))
    newProduct.publishTime = null
    const result = updateProductApi(newProduct)
    result.then(res=>{
      if (res.data){
        alert('更新成功')
      }
    })
  }
  //刷新
  const refreshAll = ()=>{
    products.value = []
    pageParams.value.startIndex = -10
    setTimeout(()=>{
      appendProducts()
    },200)
  }

</script>

<template>
  <div class="appearing-container">
    <!--新发布-->
    <div class="new-pruduct">
      <h1>发布闲置</h1>
      <div class="content-container">
        <!--标题、价格-->
        <div class="content-line">
          <h2>基本信息</h2>
          <div>
            <h3>标题</h3>
            <input  v-model="newProduct.productTitle" type="text">
          </div>
          <div>
            <h3>价格</h3>
            <input v-model="newProduct.productPrice" type="number">
          </div>
        </div>
        <!--图片-->
        <div class="content-line">
          <h2>上传图片</h2>
          <input @change="handleUploadImg" ref="fileInput" type="file" style="display: none">
          <button @click="uploadImg">上传单张图片</button>
          <div>
            <img :src="newProduct.productImgs">
          </div>
        </div>
        <!--地址-->
        <div class="content-line">
          <h2>地址</h2>
          <h3>详细地址（到宿舍）</h3>
          <input v-model="newProduct.location" type="text">
        </div>
        <!--介绍-->
        <div class="content-line">
          <h2>商品介绍</h2>
          <textarea v-model="newProduct.productIntro"></textarea>
        </div>
        <!--按钮-->
        <button class="publish-btn" @click="publishProduct">发布</button>
      </div>
    </div>
    <!--已发布-->
    <div class="my-products">
      <h1>已发布</h1>
      <div class="content-container">
        <!--刷新-->
        <div class="refresh-btn" @click="refreshAll">刷新</div>
        <div v-for="(item, index) in products" :key="index" class="procuct-container">
          <!--背景-->
          <div class="block-back">
            <img :src="item.productImgs">
            <div></div>
          </div>
          <!--图片-->
          <div class="block-img">
            <img :src="item.productImgs">
          </div>
          <!--基础信息容器-->
          <div class="block-basic">
            <div @click="deleteProduct(item.productId)" style="font-size: 13px; text-decoration: underline; align-self: end; cursor: pointer; margin-top: -8px">删除</div>
            <div style="display: flex; align-items: center">
              标题：
              <input v-model="item.productTitle">
            </div>
            <div style="display: flex; align-items: center">
              价格：
              <input type="number" v-model="item.productPrice">
            </div>
            <h4>发布时间：{{ item.publishTime }}</h4>
            <div style="display: flex; align-items: center">
              地址：
              <input v-model="item.location">
            </div>
            <button @click="contactWithSeller(item.productId)" v-if="item.status === 2">联系买家</button>
            <button v-else style="background: linear-gradient(to right bottom, #ccc, #e1d0ce);">出售中</button>
            <button @click="updateProduct(index)" v-if="item.status === 1">确定更改</button>
            <button v-else @click="reSell(item.productId)">重新出售</button>
          </div>
          <!--介绍容器-->
          <textarea class="block-intro" v-model="item.productIntro"></textarea>
        </div>
        <!--更多-->
        <div class="more-btn" @click="appendProducts">- 更多 -</div>
      </div>
    </div>
  </div>
</template>

<style scoped>
  h1{
    font-size: 20px;
    font-weight: bold;
    color: #383838;
  }
  .new-pruduct{
    position: absolute;
    left: 0;
    top: 20px;
    bottom: 20px;
    background-color: #fafafa;
    right: 54%;
    border-radius: 12px;
    box-shadow: 0 0 12px #00000033;
    padding: 8px 12px;
  }
  .my-products{
    position: absolute;
    right: 12px;
    top: 20px;
    bottom: 20px;
    width: calc(54% - 32px);
    padding: 8px;
  }
  .content-container{
    position: absolute;
    left: 16px;
    top: 48px;
    right: 16px;
    bottom: 8px;
  }
  /*新发布商品*/
  .new-pruduct .content-container{
    display: flex;
    flex-direction: column;
    align-items: center;
  }
  .new-pruduct .content-container .content-line{
    width: 100%;
    position: relative;
    margin: 4px 0;
    min-height: 32px;
  }
  .new-pruduct .content-container .content-line h2{
    font-size: 16px;
    font-weight: bold;
    color: #666;
    position: absolute;
    left: 0;
    top: 0;
  }
  /*基本信息*/
  .new-pruduct .content-container .content-line:nth-child(3),
  .new-pruduct .content-container .content-line:nth-child(2),
  .new-pruduct .content-container .content-line:nth-child(4),
  .new-pruduct .content-container .content-line:nth-child(1){
    display: flex;
    padding-top: 28px;
    height: 72px;
    align-items: center;
    justify-content: space-between;
  }
  .new-pruduct .content-container .content-line:nth-child(1) div{
    display: flex;
    width: 48%;
    height: 28px;
    align-items: center;
    justify-content: start;
  }
  .new-pruduct .content-container .content-line:nth-child(3) h3,
  .new-pruduct .content-container .content-line:nth-child(1) div h3{
    margin-bottom: 0;
    font-size: 16px;
    margin-right: 8px;
  }
  .new-pruduct .content-container .content-line:nth-child(3) input,
  .new-pruduct .content-container .content-line:nth-child(1) div input{
    border: 1px solid #ccc;
    padding: 4px 8px;
    outline: none;
    border-radius: 4px;
  }
  /*上传图片*/
  .new-pruduct .content-container .content-line:nth-child(2){
    height: 18vw;
    display: flex;
    align-items: center;
    justify-content: space-around;
  }
  .new-pruduct .content-container .content-line:nth-child(2) div{
    height: calc(100% - 8px);
    width: calc(100% - 240px);
    overflow: hidden;
    background-color: #f8f8f8;
    border-radius: 8px;
    box-shadow: inset 0 0 8px #00000033;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
  }
  .new-pruduct .content-container .content-line:nth-child(2) div img{
    max-height: 100%;
    max-width: 100%;
    object-fit: contain;
    border: 1px solid #eee;
    border-radius: 4px;
  }
  .new-pruduct .content-container .content-line:nth-child(2) button{
    padding: 8px 24px;
    outline: none;
    border: none;
    box-shadow: 0 0 4px #00000033;
    background-color: #ffffff;
    color: #000;
    cursor: pointer;
    border-radius: 4px;
    transition: .4s;
  }
  /*地址*/
  .new-pruduct .content-container .content-line:nth-child(3) input{
    flex: 0.9;
  }
  /*介绍*/
  .new-pruduct .content-container .content-line:nth-child(4){
    flex: 1;
  }
  .new-pruduct .content-container .content-line:nth-child(4) textarea{
    width: 100%;
    height: calc(100% - 32px);
    border: 1px solid #ccc;
    border-radius: 4px;
    resize: none;
    padding: 8px;
    outline: none;
  }
  /*按钮*/
  .publish-btn{
    width: 100%;
    padding: 8px 0;
    outline: none;
    border: none;
    box-shadow: 2px 2px 4px #00000033;
    background: linear-gradient(to right bottom, #9bafe5, #bba1e3);
    color: #fff;
    cursor: pointer;
    border-radius: 4px;
    transition: .4s;
    margin-bottom: 8px;
  }
  .publish-btn:hover{
    transition: .4s;
    transform: rotateX(16deg);
    box-shadow: 4px 4px 12px #799be588;
  }
  /*已发布*/
  .my-products .content-container{
    overflow-y: scroll;
    overflow-x: hidden;
  }
  .my-products .content-container .procuct-container{
    width: 100%;
    margin-bottom: 12px;
    height: 460px;
    border-radius: 16px;
    box-shadow: 2px 2px 6px #00000033;
    overflow: hidden;
    margin-right: 4px;
    position: relative;
    min-width: 600px;
  }
  .my-products .content-container .procuct-container .block-back{
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
  }
  .my-products .content-container .procuct-container .block-back div{
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    background-color: #fafafaaa;
    backdrop-filter: blur(4px);
  }
  .my-products .content-container .procuct-container .block-back img{
    min-width: 100%;
    min-height: 100%;
    position: absolute;
    object-fit: cover;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
  }
  /*图片*/
  .my-products .content-container .procuct-container .block-img{
    position: absolute;
    left: 12px;
    top: 12px;
    height: 270px;
    width: 360px;
    background-color: #fafafa;
    border-radius: 4px;
    border: #eee solid 2px;
    box-shadow: inset 0 0 8px #00000033;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
  }
  .my-products .content-container .procuct-container .block-img img{
    max-height: 100%;
    max-width: 100%;
    position: absolute;
    object-fit: contain;
  }
  /*基础信息*/
  .my-products .content-container .procuct-container .block-basic{
    position: absolute;
    left: 386px;
    right: 12px;
    top: 12px;
    height: 270px;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
  }
  .my-products .content-container .procuct-container .block-basic input{
    border: 1px solid #ccc;
    outline: none;
    padding: 6px 8px;
    border-radius: 4px;
    width: calc(100% - 52px);
    align-self: end;
    position: relative;
    background-color: #ffffffaa;
    backdrop-filter: blur(4px);
  }
  .my-products .content-container .procuct-container textarea{
    border: 1px solid #ccc;
    outline: none;
    padding: 8px;
    border-radius: 4px;
    resize: none;
    background-color: #ffffffaa;
    backdrop-filter: blur(4px);
  }

  .my-products .content-container .procuct-container .block-basic button{
    padding: 8px 24px;
    outline: none;
    border: none;
    box-shadow: 2px 2px 4px #00000033;
    background: linear-gradient(to right bottom, #9bafe5, #bba1e3);
    color: #fff;
    cursor: pointer;
    border-radius: 4px;
    transition: .4s;
  }
  .my-products .content-container .procuct-container .block-basic button:hover{
    transform: rotateX(16deg);
    box-shadow: 4px 4px 12px #799be588;
    transition: .4s;
  }

  /*介绍信息*/
  .my-products .content-container .procuct-container .block-intro{
    position: absolute;
    top: 296px;
    bottom: 12px;
    left: 12px;
    right: 12px;
    padding: 8px;
    white-space: pre-line;
    overflow:scroll;
  }
  /*更多*/
  .more-btn{
    width: 100%;
    text-align: center;
    cursor: pointer;
  }
  /*刷新*/
  .refresh-btn{
    position: fixed;
    right: 24px;
    top: 24px;
    font-size: 15px;
    text-decoration: underline;
    cursor: pointer;
  }

</style>